package com.study;

import com.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Test02 {
    public static void main(String[] args) throws SQLException {
        method05();
    }

    public static void method05() throws SQLException {
        Connection conn = JDBCUtils.getConnection();
        String sql = "select * from tb_users where username like ?";
        PreparedStatement pstat = conn.prepareStatement(sql);
        pstat.setString(1,"%a%");
        ResultSet rs = pstat.executeQuery();
        while(rs.next()){
            int uid = rs.getInt("uid");
            String username = rs.getString("username");
            String password = rs.getString("password");
            System.out.println(username + "---" + password);
        }
        JDBCUtils.close(rs, pstat, conn);
    }

    public static void method04() throws SQLException {
        //获得链接
        Connection conn = JDBCUtils.getConnection();
        //书写sql，获得执行sql的对象
        String sql = "update tb_users set password = ? where uid = ?";
        PreparedStatement pstat = conn.prepareStatement(sql);
        pstat.setString(1,"123456");
        pstat.setInt(2,3);
        //执行sql语句
        int i = pstat.executeUpdate();
        //处理结果
        System.out.println("受影响的行数是" + i);
        //释放资源
        JDBCUtils.close(null,pstat,conn);
    }

    public static void method03() throws SQLException {
        //获得链接
        Connection conn = JDBCUtils.getConnection();
        //获得执行sql的对象，写sql语句
        String sql = "delete from tb_users where uid = ?";
        PreparedStatement pstat = conn.prepareStatement(sql);
        //执行sql语句
        pstat.setInt(1,4);
        int i = pstat.executeUpdate();
        //处理结果
        System.out.println("受影响的行数是" + i);
        //释放资源
        JDBCUtils.close(null,pstat,conn);
    }

    public static void method02() throws SQLException {
        //获得链接
        Connection conn = JDBCUtils.getConnection();
        //获得执行sql的对象，写sql语句
        String sql = "INSERT INTO tb_users VALUES (?,?,?)";
        PreparedStatement pstat = conn.prepareStatement(sql);
        //执行sql
        pstat.setInt(1,5);
        pstat.setString(2,"april");
        pstat.setString(3,"123456");
        int i = pstat.executeUpdate();
        //处理结果
        System.out.println(i);
        //释放资源
        JDBCUtils.close(null,pstat,conn);
    }

    public static void method01() throws SQLException {
        // 1. 注册驱动 (无需，因为在工具类中写了注册驱动代码)
        // 2. 获得链接
        Connection conn = JDBCUtils.getConnection();
        // 3. 获得执行mysql的对象
        String sql = "select * from tb_users";
        PreparedStatement pstat = conn.prepareStatement(sql);
        // 4. 执行mysql
//        pstat.setString(1,"admin");
        ResultSet rs = pstat.executeQuery();
        // 5. 处理结果
        while(rs.next()){
            int uid = rs.getInt("uid");
            String username = rs.getString("username");
            String password = rs.getString("password");
            System.out.println(uid + "---" + username + "---" + password);
        }
        // 6. 释放资源
        JDBCUtils.close(rs, pstat, conn);
    }
}
